package com.excel.poi.ali;

import com.excel.poi.pojo.PoiExcel;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author zw
 * @Description 描述
 * @create /2022/3/25
 */
@Data
public class ExcelHeadProperty {
    /**
     * Custom class
     */
    private Class<?> headClazz;

    /**
     * 描述头部的二维数组
     */
    private List<List<String>> head = new ArrayList<List<String>>();

    /**
     * 头部描述的属性
     */
    private List<ExcelColumnProperty> columnPropertyList = new ArrayList<ExcelColumnProperty>();

    /**
     * 头部描述的属性
     */
    private Map<Integer, ExcelColumnProperty> excelColumnPropertyMap1 = new HashMap<Integer, ExcelColumnProperty>();
    private Map<String, Field> fieldMap = new HashMap<String, Field>();
    //按照解锁excel顺序
    Map<Integer, Field> linkFieldMap = new HashMap<Integer, Field>();
    private List<String> mustList = new ArrayList<String>();
    public ExcelHeadProperty(Class<? > headClazz, List<List<String>> head) {
        this.headClazz = headClazz;
        this.head = head;
        initColumnProperties();
    }

    /**
     */
    private void initColumnProperties() {
        if (this.headClazz != null) {
            List<Field> fieldList = new ArrayList<Field>();
            Class tempClass = this.headClazz;
            //When the parent class is null, it indicates that the parent class (Object class) has reached the top
            // level.
            while (tempClass != null) {
                fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
                //Get the parent class and give it to yourself
                tempClass = tempClass.getSuperclass();
            }
            List<List<String>> headList = new ArrayList<List<String>>();
            for (Field f : fieldList) {
                initOneColumnProperty(f);
            }
            //对列排序
            Collections.sort(columnPropertyList);
            if (head == null || head.size() == 0) {
                for (ExcelColumnProperty excelColumnProperty : columnPropertyList) {
                    headList.add(excelColumnProperty.getHead());
                }
                this.head = headList;
            }
        }
    }

    /**
     * @param f
     */
    private void initOneColumnProperty(Field f) {
        PoiExcel p = f.getAnnotation(PoiExcel.class);
        ExcelColumnProperty excelHeadProperty = null;
        if (p != null) {
            excelHeadProperty = new ExcelColumnProperty();
            excelHeadProperty.setField(f);
            excelHeadProperty.setHead(Arrays.asList(p.name()));
            excelHeadProperty.setIndex(p.orderNum());
            excelHeadProperty.setFormat(p.importFormat());
            excelColumnPropertyMap1.put(p.orderNum(), excelHeadProperty);
            fieldMap.put(p.name(),f);
            if(p.importIsMust()){
                mustList.add(p.name());
            }
        }
        if (excelHeadProperty != null) {
            this.columnPropertyList.add(excelHeadProperty);
        }

    }

    /**
     *
     */
    public void appendOneRow(List<String> row) {

        for (int i = 0; i < row.size(); i++) {
            List<String> list;
            if (head.size() <= i) {
                list = new ArrayList<String>();
                head.add(list);
            } else {
                list = head.get(0);
            }
            list.add(row.get(i));
        }

    }
}
